package org.jeecg.modules.yusuan.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.JavaDelegate;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.yusuan.entity.CaiwuBiaozhun;
import org.jeecg.modules.yusuan.entity.CaiwuNz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("caiwuNzEndListener")
public class CaiwuNzEndListener implements ExecutionListener {

    @Autowired
    private ICaiwuNzService caiwuNzService;
    @Autowired
    private ICaiwuBiaozhunService caiwuBiaozhunService;

    @Override
    public void notify(DelegateExecution execution) {
        String processInstanceId =  execution.getProcessInstanceId();
        CaiwuNz caiwuNz = caiwuNzService.getOne(new LambdaQueryWrapper<CaiwuNz>().eq(CaiwuNz::getProcessInstanceId,processInstanceId));
        String jingfeiType = caiwuNz.getJingfeiType();
        Integer version = caiwuNz.getVersion();
        String deptId = caiwuNz.getDeptId();
        if(StringUtils.equals("biaozhun",jingfeiType)){
            caiwuBiaozhunService.update(new CaiwuBiaozhun().setEffective(1),new LambdaUpdateWrapper<CaiwuBiaozhun>().eq(CaiwuBiaozhun::getDeptId,deptId).eq(CaiwuBiaozhun::getVersion,version));
        }

        caiwuNz.setProcessStatus(3);
        caiwuNzService.updateById(caiwuNz);

    }

}
